package com.zenithmind.article.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenithmind.article.mapper.CategoryMapper;
import com.zenithmind.article.pojo.domain.Category;
import com.zenithmind.article.pojo.query.CategoryQuery;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.article.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Override
    public Category addOrUpdateCategory(Category category) {
        if (StringUtils.isBlank(category.getId())) {
            save(category);
        } else {
            updateById(category);
        }
        return category;
    }

    @Override
    public PageResult<Category> pageCategory(CategoryQuery categoryQuery) {
        Page<Category> page = categoryQuery.toPage();
        Page<Category> page1 = lambdaQuery()
                .eq(StringUtils.isNotBlank(categoryQuery.getName()), Category::getName, categoryQuery.getName())
                .page(page);
        return new PageResult<>(page1.getRecords(), page1.getTotal(), page1.getCurrent(), page1.getSize());
    }
}
